import request from "@/utils/request";

// 查询视频列表
export function listVideo(query) {
  return request({
    url: "/system/video/list",
    method: "get",
    params: query,
  });
}

// 查询视频详细
export function getVideo(videoId) {
  return request({
    url: "/system/video/" + videoId,
    method: "get",
  });
}

// 新增视频
export function addVideo(data) {
  return request({
    url: "/system/video",
    method: "post",
    data: data,
  });
}

// 修改视频
export function updateVideo(data) {
  return request({
    url: "/system/video",
    method: "put",
    data: data,
  });
}

// 删除视频
export function delVideo(videoId) {
  return request({
    url: "/system/video/" + videoId,
    method: "delete",
  });
}

// 上传视频
export function uploadVideoFile(trainingId, videoId, data) {
  return request({
    url: `/system/video/upload/${trainingId}/${videoId}`,
    method: "post",
    headers: { "Content-Type": "application/x-www-form-urlencoded" },
    data,
  });
}

// 获取已上传分片列表
export function getUploadedChunks(params) {
  return request({
    url: "/system/video/uploadedChunks",
    method: "get",
    params: params, // 参数格式 { trainingId, videoId, fileMd5 }
  });
}

// 上传分片
export function uploadChunk(trainingId, videoId, data) {
  return request({
    url: `/system/video/uploadChunk/${trainingId}/${videoId}`,
    method: "post",
    headers: { "Content-Type": "multipart/form-data" }, // 关键修改
    data: data, // 需包含文件分片+元数据
  });
}

// 合并分片
export function mergeChunks(trainingId, videoId, data) {
  return request({
    url: `/system/video/mergeChunks/${trainingId}/${videoId}`,
    method: "post",
    data: data, // { fileMd5, fileName, totalChunks }
  });
}
